//
//  MainViewController.swift
//  PopViewMenu
//
//  Created by Rookie_YX on 16/5/27.
//  Copyright © 2016年 YX_Rookie. All rights reserved.
//

import UIKit

class MainViewController: UIViewController {

    // 记录当前UIPresentationController是否展开
    var isPresent:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        navigationItem.title = "首页"
        
        // 初始化导航条
        setupNav()
        // 注册通知,监听菜单
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MainViewController.change), name: YXRPopoverAnimatorWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MainViewController.change), name: YXRPopoverAnimatorWillDismiss, object: nil)

    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK:-
    // MARK:-   自定义函数
  
    private func setupNav(){
    
        // 初始化左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.creaBarButtonItem("navigationbar_friendattention", target: self, action:  #selector(MainViewController.leftBarClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem.creaBarButtonItem("navigationbar_pop", target: self, action:  #selector(MainViewController.rightBarClick))
        
        // 初始化标题按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("Rookie_YX", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: #selector(MainViewController.clickNavTitleBtn(_:)), forControlEvents: UIControlEvents.TouchUpInside)
         navigationItem.titleView = titleBtn
    }
    
    // MARK:-
    // MARK:- 通知事件
    func change(){
        let titleBtn = navigationItem.titleView as! TitleButton
        titleBtn.selected = !titleBtn.selected
    }
    // MARK:-
    // MARK:-  导航栏按钮点击事件
    func leftBarClick(){
        
    }
    
    func rightBarClick(){
        
    }
    
    // MARK:-
    // MARK:-  导航栏标题按钮点击事件
    func clickNavTitleBtn(btn:UIButton){
        // 修改箭头方向
        btn.selected = !btn.selected

        /**
         *  弹出菜单
         */
        let storyB = UIStoryboard(name:"PopoverView", bundle: nil)
        let VC = storyB.instantiateInitialViewController()
        VC?.transitioningDelegate = popoverAnimator
        VC?.modalPresentationStyle = UIModalPresentationStyle.Custom
        presentViewController(VC!, animated: true, completion: nil)
    }
    
    // MARK:-
    // MARK:-  懒加载
    // popViewAnimation
    private lazy var popoverAnimator:PopoverAnimator = {
        let popover = PopoverAnimator()
        popover.presentFrame = CGRect(x: 100, y: 50, width: 150, height: 200)
        return popover
    }()
}
